{% extends 'base.exported.class' %}

{% block content %}
package main

{% if is_binary %}
import (
	{% if is_test or to_json %}
	"encoding/json"
	{% endif %}
	"fmt"
  "io/ioutil"
	"os"
	"strconv"
)

type {{ class_name }} struct {
	Coeffs []float64 `json:"coeffs"`
	Inters float64 `json:"inters"`
}
{% else %}
import (
	{% if is_test or to_json %}
	"encoding/json"
	{% endif %}
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"strconv"
)

type {{ class_name }} struct {
	Coeffs [][]float64 `json:"coeffs"`
	Inters []float64 `json:"inters"`
}
{% endif %}

{% if is_test or to_json %}
type Response struct {
	Predict int `json:"predict"`
}
{% endif %}

{% if is_binary %}
func (svc {{ class_name }}) Predict(features []float64) int {
	var prob float64
	for i := 0; i < len(svc.Coeffs); i++ {
		prob = prob + svc.Coeffs[i] * features[i]
	}
	if (prob + svc.Inters) > 0 {
		return 1
	}
	return 0
}
{% else %}
func (svc {{ class_name }}) Predict(features []float64) int {
	classIdx := 0
	classVal := math.Inf(-1)
	outerCount, innerCount := len(svc.Coeffs), len(svc.Coeffs[0])
	for i := 0; i < outerCount; i++ {
		var prob float64
		for j := 0; j < innerCount; j++ {
			prob = prob + svc.Coeffs[i][j] * features[j]
		}
		if prob + svc.Inters[i] > classVal {
			classVal = prob + svc.Inters[i]
			classIdx = i
		}
	}
	return classIdx
}
{% endif %}

func main() {

	// Features:
	var features []float64
	for _, arg := range os.Args[2:] {
		if n, err := strconv.ParseFloat(arg, 64); err == nil {
			features = append(features, n)
		}
	}

	// Model data:
	jsonFile, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Println(err)
	}
	defer jsonFile.Close()
	byteValue, _ := ioutil.ReadAll(jsonFile)

	// Estimator:
	var clf {{ class_name }}
	json.Unmarshal(byteValue, &clf)

	{% if is_test or to_json %}
	// Get JSON:
	prediction := clf.Predict(features)
	res, _ := json.Marshal(&Response{Predict: prediction})
	fmt.Println(string(res))
	{% else %}
	// Get class prediction:
	prediction := clf.Predict(features)
	fmt.Printf("Predicted class: #%d\n", prediction)
	{% endif %}

}
{% endblock %}
